package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Warning;
import com.sdry.model.llm.User;
import com.sdry.service.hpf.WarningService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: WarningController 
 * @Description:预警管理Controller
 * @Author hpf
 * @Date 2018年9月25日 上午9:20:37 
 * @version 1.0
 */
@RequestMapping(value="/warningMana")
@Controller
public class WarningController {

	@Resource
	private WarningService warningService;
	
	/**
	 * 分页查询预警管理
	 * @param page 分页条件
	 * @param warning 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageWarning")
	public void listPageWarning(Page page,Warning warning,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (warning.getWnum() != null) {
			map.put("wnum", warning.getWnum());
		} else {
			map.put("wnum", "");
			warning.setWnum("");
		}
		int count = warningService.countWarning(warning);
		List<Warning> warningList = warningService.listPageWarning(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(warningList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存预警管理
	 * @param warning
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveWarning",method=RequestMethod.POST)
	public void saveWarning(Warning warning,HttpServletResponse response, HttpSession session) throws Exception{
		User user = (User)session.getAttribute("currentUser");
		//获取当前用户作为设置人
		if(user == null || "".equals(user)){
			warning.setWpersonID("null");
		}else{
			warning.setWpersonID(user.getUserName());
		}
		int isSuccess = 0;
		if (warning.getWid() == null || "".equals(warning.getWid())) {
			isSuccess = warningService.insertWarning(warning);
		} else {
			isSuccess = warningService.updateWarning(warning);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除预警管理
	 * @param wid 预警管理ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteWarning")
	public void deleteWarning(Long wid,HttpServletResponse response) throws Exception{
		int isSuccess = warningService.deleteWarning(wid);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 批量删除预警管理
	 * @param idArr 预警管理ID数组
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteWarningByArr")
	public void deleteWarningByArr(@RequestParam("idArr[]") String[] idArr,HttpServletResponse response) throws Exception{
		int isSuccess = warningService.deleteWarnings(idArr);
		ResponseUtil.write(response, isSuccess);
	}
}
